home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2306 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  59 lines

  1. Path: comsearch.com!tnasca
  2. From: Thuan Nguyen <thnguyen@comsearch.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: virtual function in template class
  5. Date: 16 Jan 1996 23:01:17 GMT
  6. Organization: ComSearch, Inc
  7. Message-ID: <4dhant$gj8@gateway.comsearch.com>
  8. NNTP-Posting-Host: arcturus.comsearch.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.4 sun4c)
  13. X-URL: news:comp.lang.c++
  14.  
  15. Hi all, I have a question. Can I have virtual function in template class. My
  16. template class definition is as follows:
  17.  
  18. template <class T>
  19. class Mine 
  20. {
  21. public:
  22.    void compare_data(T & );
  23.    virtual T * build_data() {}
  24.    ....
  25. }
  26.  
  27. class first_data: public Mine<myDataType1>
  28. {
  29. public:
  30.    myDataType1 * build_data();
  31.    ....
  32. }
  33.  
  34. class second_data : public Mine<myDataType2>
  35. {
  36. public:
  37.    myDataType2 * build_data();
  38.    ....
  39. }    
  40.  
  41. template< class T>
  42. void Mine<T>::compare_data(T & newData)
  43. {
  44.     ....
  45.     T * oldData = build_data();
  46.     if ( newData == *oldData) return TRUE;
  47.     ....    
  48. }
  49.  
  50. eventhough function compare data implementation is in template class Mine. It
  51. would never be called from there. It will be called only from either class
  52. first_data or second_data in run time. At that time virtual build_data should
  53. have been resolved.
  54.  
  55. I have a lots of problem with this including implecit declaration of build_data
  56.  
  57. Thanks
  58.  
  59.